<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/9/5
 * Time: 12:01
 */

namespace app\model\novel;


use app\common\lib\Common;
use app\exception\IndexException;
use app\model\BaseModel;


class NovelSpreadModel extends BaseModel
{
    protected $table = 'video_novel_spread';

    protected $primaryKey = 'id';

    public $timestamps = false;

    protected static $instance;

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function saveData($param)
    {
        if(!empty($param[$this->primaryKey])){
            $info = $this->find($param[$this->primaryKey]);
            if($info==false){
                throw new IndexException(4020,'数据不存在');
            }

            $data = [];
            !empty($param['title']) && $data['title'] = $param['title'];
            !empty($param['main_image']) && $data['main_image'] = $param['main_image'];
            !empty($param['platform']) && $data['platform'] = $param['platform'];
            !empty($param['business_id']) && $data['business_id'] = $param['business_id'];
            !empty($param['episode_num']) && $data['episode_num'] = $param['episode_num'];
            !empty($param['is_end']) && $data['is_end'] = $param['is_end'];
            isset($param['labels']) && $data['labels'] = $param['labels'];
            isset($param['desc']) && $data['desc'] = $param['desc'];

            !empty($param['data_source_key']) && $data['data_source_key'] = $param['data_source_key'];
            !empty($param['data_source_id']) && $data['data_source_id'] = $param['data_source_id'];
            !empty($param['created_at']) && $data['created_at'] = $param['created_at'];

            isset($param['virtual_income']) && $data['virtual_income'] = $param['virtual_income'];
            isset($param['virtual_income']) && $data['total_income'] = round($param['virtual_income']+$info['really_income']);

            isset($param['virtual_highest_income']) && $data['virtual_highest_income'] = $param['virtual_highest_income'];
            isset($param['virtual_highest_income']) && $data['total_heighest_income'] = round($param['virtual_highest_income']+$info['really_heighest_income']);

            isset($param['virtual_spread_num']) && $data['virtual_spread_num'] = $param['virtual_spread_num'];
            isset($param['virtual_spread_num']) && $data['total_spread_num'] = round($param['virtual_highest_income']+$info['really_spread_num']);

            isset($param['status']) && $data['status'] = $param['status'];
            isset($param['sort']) && $data['sort'] = $param['sort'];

            !empty($param['isdel']) && $data['isdel'] = $param['isdel'];

            $data['updatetime'] = date('Y-m-d H:i:s');
            return $this->where(['novel_id'=>$param['novel_id']])->update($data);
        }else{
            $data = [];
            if(empty($param['novel_sn'])){
                $maxId =  $this->where([['novel_sn','>',0]])->max('novel_sn');
                $data['novel_sn'] = Common::getSnCode($maxId,'50',3,1);
            }else{
                $data['novel_sn'] = $param['novel_sn'];
            }

            $data['title'] = $param['title']??'';
            $data['main_image'] = $param['main_image']??'';
            $data['platform'] = $param['platform']??'1';
            $data['business_id'] = $param['business_id']??'';
            $data['labels'] = $param['labels']??'';
            $data['desc'] = $param['desc']??'';

            $data['virtual_income'] = $param['virtual_income']??'0';
            $data['total_income'] = $param['virtual_income']??'0';

            $data['virtual_highest_income'] = $param['virtual_highest_income']??'0';
            $data['total_heighest_income'] = $param['virtual_highest_income']??'0';

            $data['virtual_spread_num'] = $param['virtual_spread_num']??'0';
            $data['total_spread_num'] = $param['virtual_spread_num']??'0';

            $data['data_source_key'] = $param['data_source_key']??'';
            $data['data_source_id'] = $param['data_source_id']??'0';
            $data['episode_num'] = $param['episode_num']??'0';
            $data['is_end'] = $param['is_end']??'0';
            $data['start_time'] = $param['start_time']??null;

            $data['app_id'] = $param['app_id']??'';
            $data['task_id'] = $param['task_id']??'';
            $data['task_start_time'] = $param['task_start_time']??null;
            $data['task_end_time'] = $param['task_end_time']??null;
            $data['task_settle_type'] = $param['task_settle_type']??'0';
            $data['payment_allocate_ratio'] = $param['payment_allocate_ratio']??'0';
            $data['task_refund_period'] = $param['task_refund_period']??'0';
            $data['task_status'] = $param['task_status']??'0';

            $data['first_category'] = $param['first_category']??'';
            $data['second_category'] = $param['second_category']??'';
            $data['words_num'] = $param['words_num']??'';
            $data['operate_tags'] = $param['operate_tags']??'';
            $data['author'] = $param['author']??'';
            $data['book_id'] = $param['book_id']??'';
            $data['spread_type'] = $param['spread_type']??'1';
            $data['under_status'] = $param['under_status']??'1';
            $data['lang'] = $param['lang']??'';

            isset($param['status']) && $data['status'] = $param['status'];

            $id = $this->insertGetId($data);

            return $id;
        }
    }
}